﻿using BST250301;
using BST250301.Pages;
using BST250301TestStand.Models.Utils;
using OpenTK.Audio.OpenAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BST250301TestStand.Models
{
    internal class EngineConfig
    {
        public event EventHandler<MonitorItem> ValueChanged;
        private MonitorItem _itemValue =new MonitorItem();
        public MonitorItem ItemValue
        {
            get => _itemValue;
            set
            {
                if (_itemValue.TemperatureOil != value.TemperatureOil || _itemValue.MasterGearPosition != value.MasterGearPosition || _itemValue.SlaveGearPosition != value.SlaveGearPosition || _itemValue.CFStatus != value.CFStatus || _itemValue.CRStatus != value.CRStatus || _itemValue.CPTOStatus != value.CPTOStatus || _itemValue.LoadeSeeped != value.LoadeSeeped || _itemValue.RotateSeeped != value.RotateSeeped)
                {
                    _itemValue = value;
                    ValueChanged?.Invoke(this, _itemValue);
                }
            }
        }
        private static PLCConfig plc;
        /// <summary>
        /// 是否允许加温
        /// </summary>
        private static bool isEnable;
        private PLCData oildataset =new PLCData { 
        PLCAdr ="M102",
        PLCType="float"
        };
        private PLCData oildataget = new PLCData
        {
            PLCType = "float",
            PLCAdr="M100",
        };
        public bool SetOil(int setvalue,int diffvalue,int actionvalue)
        {
            plc =MainWindow.PLC;
            int max = setvalue + diffvalue;
            int min = setvalue - diffvalue;
            int actionMax = setvalue + actionvalue;
            int actionMin = setvalue - actionvalue;
            if (!PLCConfig.IsConnectPLC)
                return false;
            Task.Run(() => {
                while (true)
                {
                    Thread.Sleep(1000 * 1);
                    var res = GetOilValue(oildataget);
                    float result = (float)res;
                    var item = ItemValue;
                    item.TemperatureOil = result;
                    ItemValue = item;
                    if (!isEnable)
                        continue;
                    if (result >= min)
                    {                        
                        //超过最小值
                    }
                    if (result >= setvalue)
                    {
                        //达到设定值  停止加温
                        SetOilOn(oildataset);
                    }
                    if (result >= max)
                    {
                        //高于最大值 报警
                    }
                    if (result < min)
                    {
                        //低于最小值 报警
                    }
                    if (result < setvalue)
                    {
                        //低于设定值 加温
                        SetOilOff(oildataset);
                    }
                }
            });
            return true;
        }
        private void SetOilOn(PLCData oildataset)
        {
             var res = plc.WritePLCAdrWithType(oildataset.PLCAdr, oildataset.PLCType,"true");
        }
        private void SetOilOff(PLCData oildataset)
        {
            var res = plc.WritePLCAdrWithType(oildataset.PLCAdr, oildataset.PLCType, "false");
        }
        private object GetOilValue(PLCData oildataget)
        {
            return plc.ReadPLCWithType(oildataget.PLCAdr, oildataget.PLCType);
        }
    }
}
